package com.wancaijinfu.component;

import com.wancaijinfu.actitvity.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

public class MSureDialog extends Dialog {
	private TextView sure;
	private TextView cancel;
	private TextView content;

	private String contentText;
	private String sureText;
	private String cancelText;

	private View.OnClickListener sureListener;
	private View.OnClickListener cancelListener;

	public MSureDialog(Context context, int theme) {
		super(context, theme);
	}

	public MSureDialog(Context context, int theme,
			View.OnClickListener sureListener,
			View.OnClickListener cancelListener) {
		super(context, theme);
		this.sureListener = sureListener;
		this.cancelListener = cancelListener;
	}

	public MSureDialog(Context context, int theme, String contentText,
			String sureText, String cancelText) {
		super(context, theme);
		this.contentText = contentText;
		this.sureText = sureText;
		this.cancelText = cancelText;
	}

	public MSureDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.msuredialog_content);
		sure = (TextView) findViewById(R.id.sure_button);
		cancel = (TextView) findViewById(R.id.cancel_button);
		content = (TextView) findViewById(R.id.sure_content);
		if (sureText != null)
			sure.setText(sureText);
		if (cancelText != null) {
			if (cancelText.equals("")) cancel.setVisibility(View.GONE);
			else cancel.setText(cancelText);
		}
		if (contentText != null)
			content.setText(contentText);
		
		this.setCancelable(false);
	}

	public void setListener(View.OnClickListener sureListener,
			View.OnClickListener cancelListener) {
		View.OnClickListener defaultListener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		};
		sure.setOnClickListener(sureListener == null ? defaultListener
				: sureListener);
		cancel.setOnClickListener(cancelListener == null ? defaultListener
				: cancelListener);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (listenerKey(keyCode, event))
			return false;
		return super.onKeyDown(keyCode, event);
	}
	
	protected boolean listenerKey(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			dismiss();
			return true;
		}
		return false;
	}
}
